package com.apis.util.commons;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * @author ZhangMaoren
 * @date 2012-5-23
 * @copyright IMTT. Co. Ltd.
 */
public class XmlUtil {
	public static synchronized Document newDocument() {
		Document doc = null;
		try {
			DocumentBuilderFactory buildFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder domBuilder = buildFactory.newDocumentBuilder();
			doc = domBuilder.newDocument();
		} catch (ParserConfigurationException ex) {
			ex.printStackTrace();
		}
		return doc;
	}

	public static synchronized Document loadDocument(File file) {
		BufferedInputStream is = null;
		Document document = null;
		if (file.exists()) {
			try {
				DocumentBuilderFactory buildFactory = DocumentBuilderFactory
						.newInstance();
				DocumentBuilder domBuilder = buildFactory.newDocumentBuilder();
				is = new BufferedInputStream(new FileInputStream(file));
				document = domBuilder.parse(is);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return null;
			} catch (ParserConfigurationException e) {
				e.printStackTrace();
				return null;
			} catch (SAXException e) {
				e.printStackTrace();
				return null;
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			} finally {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return document;
	}

	public static synchronized Document loadDocument(byte[] data) {
		BufferedInputStream is = null;
		Document document = null;
		try {
			if (data != null) {
				is = new BufferedInputStream(new ByteArrayInputStream(data));
				document = loadDocument(is);
			}
		} finally {
			try {
				if (is != null)
					is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return document;
	}

	public static synchronized Document loadDocument(InputStream data) {
		Document document = null;
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder domBuilder = factory.newDocumentBuilder();
			if (data != null) {
				document = domBuilder.parse(data);
			}
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
			return null;
		} catch (SAXException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally {
			try {
				if (data != null)
					data.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return document;
	}

	public static synchronized Document loadDocument(String xml) {
		Document document = null;
		Reader reader = null;
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			if (xml != null) {
				reader = new StringReader(xml);
				InputSource source = new InputSource(reader);
				document = builder.parse(source);
			}
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
			return null;
		} catch (SAXException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally {
			try {
				if (reader != null) {
					reader.close();
					reader = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return document;
	}

	public static synchronized void saveDocument(Document doc, String filePath) {
		OutputStreamWriter osp = null;
		Document docs = doc;
		try {
			TransformerFactory transFactory = TransformerFactory.newInstance();
			DOMSource source = new DOMSource(docs);
			osp = new OutputStreamWriter(new FileOutputStream(
					new File(filePath)), "UTF-8");
			StreamResult result = new StreamResult(osp);
			Transformer transformer = transFactory.newTransformer();
			transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
			transformer.transform(source, result);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} finally {
			try {
				osp.close();
			} catch (IOException ex) {
			}
		}
	}

	public static synchronized byte[] saveDocument(Document doc) {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		byte[] data = null;
		try {
			TransformerFactory transFactory = TransformerFactory.newInstance();
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(bos);
			Transformer transformer = transFactory.newTransformer();
			transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
			transformer.transform(source, result);
			data = bos.toByteArray();
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		} finally {
			try {
				if (bos != null)
					bos.close();
			} catch (IOException ex) {
			}
		}
		return data;
	}
	
}
